<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Class_Assess extends AdminController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Index";
		$this -> load ->model("Class_model");
		$this-> load ->library('ProjectService/attend_service');
		$this -> load ->model("Attend_model");
		$this -> load ->model("Attend_img_model");
		$this-> load ->library('ProjectService/class_service');
		$this -> load ->model("Admin_model");
		$this -> load -> library("ProjectService/table_service");
		$this -> load -> library("ProjectService/admin_service");
		
	}

	  //督导听课
    function attendAdd($attend_id,$teacher_id=false){
       
        $data =$this ->attend_service->getAttendData(0);
     // pri($data);
        $list=$this->admin_service->getAllAdmin();
        foreach ($list as $key => $value) {
            $arr[$key]['id'] = $value['id'];
            $arr[$key]['usercode'] = $value['usercode'];
            $arr[$key]['username'] = $value['username'];
            $arr[$key]['dept_name'] = $value['dept_name'];
        }
         $data['teacher'] = $arr;

        //获取教师职称    
        $titlelist = array();
        foreach ((new Title)->getAll() as $k => $v){
			$titlelist[$k]['key'] = $v[XPHEnum::Value];
			$titlelist[$k]['value'] = $v[XPHEnum::Caption];
        }
		$data['teacher_title'] =$titlelist;
         //获取教师职称    
        $teachers_attribute_list = array();
        foreach ((new TeachersAttribute)->getAll() as $k => $v){
			$teachers_attribute_list[$k]['key'] = $v[XPHEnum::Value];
			$teachers_attribute_list[$k]['value'] = $v[XPHEnum::Caption];
        }

        $data['teachers_attribute_list'] =$teachers_attribute_list;

        //获取课程类型 
  		$this -> load -> model("Select_model");
		$result=$this -> Select_model -> get_list(array('type'=>SelectType::CourseType[XPHEnum::Value],'status'=>CommonStatus::UnDeleted[XPHEnum::Value]));
		$data['class_type']=array();
		foreach ($result as $k => $v) {
			array_push($data['class_type'],$v['option']);
		}

	//	pri($data['class_type']);
 		
        //督导身份
       $data['AttendType']= $this->table_service->getAttendType(getSess()['id']);
		$data['is_add']=0;
      	$date=date('Y-m-d',time());			
		$month=date('m',time());			
		if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
			$year=date('Y',time());
			$year_last=date('Y',time())+1;
			$data['class_detail']['semester']='第一学期';	
			$data['class_detail']['academic_year']=strval($year.'-'.$year_last);			
		}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
			$year=date('Y',time())-1;
			$year_last=date('Y',time());
			$semester='第一学期';	
			$data['class_detail']['academic_year']=strval($year.'-'.$year_last);	
			$data['class_detail']['semester']='第二学期';
		}


		$data['title'] = "督导听课";
		//  pri($data['teacher']);
      $this->load->view('admin/attendAdd',$data);
    }

  // 0426教师信息搜索
    function getTeatcherList(){
        $this->load->model('Admin_model');
        $connar['where']=$this->sqlLikeEscape('admin_type=?? and status=?? and (username like "%??%" or usercode like "%??%" ) and role_id=??',array(AdminType::Auth[XPHEnum::Value],CommonStatus::UnDeleted[XPHEnum::Value],$_POST['teacher_name'],$_POST['teacher_code'],WebRole::Teacher[XPHEnum::Value]));
        $data=$this->Admin_model->get_single_full($connar);

        $data['title'] = (new Title)->show($data['title']);
         $data['teachers_attribute'] = (new TeachersAttribute)->show($data['teachers_attribute']);

      // pri($data);
        exit(retJson('返回搜索结果',true,$data)) ; 
    }


       // 0507课程信息搜索
    function getClassList(){
    	$teacher_code=$_POST['teacher_code'];
        $this->load->model("Class_model");
        $month=date('m',time());
        if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
            $year=date('Y',time());
            $year_last=date('Y',time())+1;
            $default_academic_year=strval($year.'-'.$year_last);            
        }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
            $year=date('Y',time())-1;
            $year_last=date('Y',time());
            $default_academic_year=strval($year.'-'.$year_last);    
        }
         
 		$connar['where'] = 'db_class.elective_lesson_no like "%'.$default_academic_year.'%"';

        if(isset($teacher_code)){
           $connar['where'] .= 'and db_class.teacher_id like "%'.$teacher_code.'%"';
        }


        $connar['join']=array('db_admin','db_admin.usercode=db_class.teacher_id');
        $connar['items']="name,db_class.id,username,db_admin.dept_name";
        $connar['groupby']="name";
        $data=$this->Class_model->get_list_full($connar);
        foreach ($data as $k => $v) {
            if(!$v['username']){
                $data[$k]['username']='';
            }
            if(!$v['dept_name']){
                $data[$k]['dept_name']='';
            }else{
                $data[$k]['dept_name']='--'.$v['dept_name'];
            }
        }

     //   pri($data);
       
        exit(retJson('返回搜索结果',true,$data)) ; 
    }


 // 0507 课程信息session保存
    function setClass(){
         $this->load->model("Class_model");
        if(isset($_POST['class_name'])){
            $_SESSION[SESS_ASSESS]['class_name']=$_POST['class_name'];
            if(isset($_SESSION[SESS_ASSESS]['teacher_code'])){
                $month=date('m',time());
                if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
                    $year=date('Y',time());
                    $year_last=date('Y',time())+1;
                    $default_academic_year=strval($year.'-'.$year_last);            
                }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
                    $year=date('Y',time())-1;
                    $year_last=date('Y',time());
                    $default_academic_year=strval($year.'-'.$year_last);    
                }
                $connar['where']=$this->sqlLikeEscape('name like "%??%" and elective_lesson_no like "%??%" and teacher_id=??',array($_POST['class_name'],$default_academic_year,$_SESSION[SESS_ASSESS]['teacher_code']));
                $data=$this->Class_model->get_single_full($connar);
                /*if($data){
                    $_SESSION[SESS_ASSESS]['classtype']=$data['type'];
                    $_SESSION[SESS_ASSESS]['classname']=$data['classname'];
                    $_SESSION[SESS_ASSESS]['course_units']=$data['course_units'];                
                    if($data['classroom']){
                        $_SESSION[SESS_ASSESS]['classroom']=explode(';',$data['classroom'])[0];
                    }
                    
                }*/
            }
            exit(retJson('保存成功',true)); 
        }
        exit(retJson('系统错误',false)); 
    }


 // 0507 课程信息session保存
    // function setClass(){
    //      $this->load->model("Class_model");
    //     if(isset($_POST['class_name'])){

    //         if(isset($_POST['teacher_code'])){
    //             $month=date('m',time());
    //             if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
    //                 $year=date('Y',time());
    //                 $year_last=date('Y',time())+1;
    //                 $default_academic_year=strval($year.'-'.$year_last);            
    //             }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
    //                 $year=date('Y',time())-1;
    //                 $year_last=date('Y',time());
    //                 $default_academic_year=strval($year.'-'.$year_last);    
    //             }
    //             $connar['where']=$this->sqlLikeEscape('name like "%??%" and elective_lesson_no like "%??%" and teacher_id=??',array($_POST['class_name'],$default_academic_year,$_POST['teacher_code']));
    //             $data=$this->Class_model->get_single_full($connar);

    //              $arraylist = array();
    //             $arraylist['classtype'] = $data['type'];
    //             $arraylist['classname'] = $data['classname'];
    //             $arraylist['course_units'] = $data['course_units'];
    //             if($data['classroom']){
    //             	 $arraylist['classroom'] = explode(';',$data['classroom'])[0];               	
    //             }
             
    //         }
    //         exit(retJson('返回数据',true, $arraylist)); 
    //     }else{
    //     	exit(retJson('系统错误',false)); 
    //     }
        
    // }



    //保存督导听课

	// function index($attend_id,$teacher_id=false){
	// 	if($teacher_id){
	// 			$data=$this->Admin_model->get_single(array('id'=>$teacher_id));
	// 			$result['teacher_detail']=$data;
	// 		}
			
	// 		if($attend_id==0){
	// 			$result=$this ->attend_service->getAttendData(0);
	// 			$result['is_add']=0;
	// 			$date=date('Y-m-d',time());			
	// 			$month=date('m',time());			
	// 			if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
	// 				$year=date('Y',time());
	// 				$year_last=date('Y',time())+1;
	// 				$result['class_detail']['semester']='第一学期';	
	// 				$result['class_detail']['academic_year']=strval($year.'-'.$year_last);			
	// 			}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
	// 				$year=date('Y',time())-1;
	// 				$year_last=date('Y',time());
	// 				$semester='第一学期';	
	// 				$result['class_detail']['academic_year']=strval($year.'-'.$year_last);	
	// 				$result['class_detail']['semester']='第二学期';
	// 			}			
	// 			if(isset($_GET['next'])){
	// 				$result['next']=$_GET['next'];
	// 			}
	// 			$result['title'] = "督导听课";
	// 			//pri($result);
	// 			$this->load->view('admin/attendAdd',$result);
	// 		}else{
	// 			$result=$this->attend_service->getAttendData($attend_id);
	// 			$result['is_add']=$attend_id;
	// 			if($result['attend']['type']==AttendType::School[XPHEnum::Value]){
	// 				$result['role_index']=0;
	// 			}else if($result['attend']['type']==AttendType::Unit[XPHEnum::Value]){
	// 				$result['role_index']=1;
	// 			}
	// 			if($teacher_id){
	// 				$data=$this->Admin_model->get_single(array('id'=>$teacher_id));
	// 				$result['teacher_detail']=$data;
	// 			}
	// 			if(isset($_GET['next'])){
	// 				$result['next']=$_GET['next'];
	// 			}
	// 			$this->load->view('admin/attendAdd',$result);
	// 		}
	// }


	// function getSelect(){		
	// 	$this -> load -> model("Select_model");
	// 	$result=$this -> Select_model -> get_list(array('type'=>SelectType::CourseType[XPHEnum::Value],'status'=>CommonStatus::UnDeleted[XPHEnum::Value]));
	// 	$data['class_type']=array();
	// 	foreach ($result as $k => $v) {
	// 		array_push($data['class_type'],$v['option']);
	// 	}
	// 	$data['title']=(new Title)->getAllOneD();
	// 	$data['age_groups']=(new AgeGroups)->getAllOneD();
	// 	$data['teachers_attribute']=(new TeachersAttribute)->getAllOneD();
	// 	// 获取身份选项
	// 	$type=(new AttendType)->getAllOneD();
	// 	$data['type']=array();
	// 	$role=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
	// 	array_push($role, $_SESSION[SESS_USER_HOME]['role_id']);		
	// 	if(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)){
	// 		// unset($data['type'][0]);
	// 		// array_splice($data['type'],0,1);  
	// 		array_push($data['type'], AttendType::School[XPHEnum::Caption]);
	// 	}
	// 	if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role)){
	// 		// unset($data['type'][1]);
	// 		// array_splice($data['type'],1,1);  
	// 		array_push($data['type'], AttendType::Unit[XPHEnum::Caption]);
	// 	}
	// 	if(in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
	// 		// unset($data['type'][1]);
	// 		// array_splice($data['type'],1,1);  
	// 		array_push($data['type'], AttendType::Outside[XPHEnum::Caption]);
	// 	}
		
		
	// 	// 获取开课单位
	// 	$date=date('Y-m-d',time());			
	// 	$month=date('m',time());
	// 	if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
	// 		$year=date('Y',time());
	// 		$year_last=date('Y',time())+1;
	// 		$default_academic_year=strval($year.'-'.$year_last);			
	// 	}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
	// 		$year=date('Y',time())-1;
	// 		$year_last=date('Y',time());
	// 		$default_academic_year=strval($year.'-'.$year_last);	
	// 	}
	// 	$connar['items']="course_units";
	// 	$connar['where']=$this->sqlLikeEscape('elective_lesson_no like "%??%" and course_units is not null',array($default_academic_year));
	// 	$connar['groupby']="course_units";
	// 	$data['course_units']=array_column($this->Class_model->get_list_full($connar), 'course_units');
	// 	//获取分数下拉列表
	// 	$result=$this ->attend_service->getAttendData(0);		
	// 	$data['teaching_content_score']=array();
	// 	$data['student_status_score']=array();
	// 	$data['teaching_implementation_score']=array();
	// 	// $score=array();
	// 	// // var_dump($result);
	// 	// // exit;
	// 	// foreach ($result['attend_teaching_content'] as $k => $v) {
	// 	// 	// foreach ($v['score'] as $k1 => $v1) {
	// 	// 	// 	array_push($data['teaching_content_score'][$k],)
	// 	// 	// }
	// 	// 	for($i=1;$i<=$v['score'];$i++){
	// 	// 		$score=array($i-1=>$i);
	// 	// 		if($i==0){
	// 	// 			$t=array_merge_recursive($score);
	// 	// 		}else{
	// 	// 			array_push($score,$i);
	// 	// 		}

	// 	// 	}
	// 	// 	var_dump($score);
	// 	foreach ($result['attend_teaching_content'] as $k => $v) {
	// 		$score=array();					
	// 		for($i=1;$i<=$result['attend_teaching_content'][$k]['score'];$i++){
	// 			$score[$i-1]=$i;
	// 		}
	// 		foreach ($score as $k2 => $v2) {						
	// 			array_push($score,$v2);
	// 		}
	// 		$data['teaching_content_score'][$k]=$score;				
	// 	}

	// 	foreach ($result['attend_student_status'] as $k => $v) {
	// 		$score=array();					
	// 		for($i=1;$i<=$result['attend_student_status'][$k]['score'];$i++){
	// 			$score[$i-1]=$i;
	// 		}
	// 		foreach ($score as $k2 => $v2) {						
	// 			array_push($score,$v2);
	// 		}
	// 		$data['student_status_score'][$k]=$score;				
	// 	}

	// 	foreach ($result['attend_teaching_implementation'] as $k => $v) {
	// 		$score=array();					
	// 		for($i=1;$i<=$result['attend_teaching_implementation'][$k]['score'];$i++){
	// 			$score[$i-1]=$i;
	// 		}
	// 		foreach ($score as $k2 => $v2) {						
	// 			array_push($score,$v2);
	// 		}
	// 		$data['teaching_implementation_score'][$k]=$score;				
	// 	}

	// 	return $data;
	// }
	// // function IndexAdd($teacher_name,$classroom,$classname){	
	// //     $result['teacher_name']=$_POST['teacher_name'];
	// //     $result['classroom']=$_POST['classroom'];
	// //     $result['classname']=$_POST['classname'];
	// // 	$this->load->view('home/class_assess',$result);
	// // }

	// function showClass($class_id){
	// 	$data=$this->class_service->getClassById($class_id);

	// 	$data['start_time']=date('H:i:s',$data['start_time']);
	// 	$data['end_time']=date('H:i:s',$data['end_time']);
	// 	// $this->load->view('home/showClass',$data);
	// 	exit(retJson("课程数据",true,$data));
	// }


	// function ClassIndex(){
	// 	// $connar['items']="DISTINCT(db_class.classroom)";
	// 	// $result['classroom'] = $this -> Class_model ->get_list_full($connar);

	// 	// $connar['items']="DISTINCT(db_class.name) classname";
	// 	// $result['classname'] = $this -> Class_model ->get_list_full($connar);
		
	// 	// $connar['items']="DISTINCT(db_admin.username) teacher_name,db_class.teacher_id";
	// 	// $connar['join']=array("db_admin","db_admin.id=db_class.teacher_id");
	// 	// $result['teacher'] = $this -> Class_model ->get_list_full($connar);
	// 	// $connar1['items']="db_class.*,db_admin.username teacher_name";
	// 	// $connar1['join']=array("db_admin","db_admin.id=db_class.teacher_id");		
	// 	// $result['detail']=$this -> Class_model ->get_list_full($connar1);
	// 	// $connar2['items']="max(end_time) max_week";
	// 	// $data=$this-> Class_model->get_single_full($connar2);
	// 	// $result['max_week']=$data['max_week'];
	// 	$data['class_name']='';
	// 	$data['teacher_name']='';
	// 	$data['class_id']='';
	// 	$data['teacher_id']='';
	// 	if(isset($_GET['class_name'])){
	// 		$data['class_name']=urldecode($_GET['class_name']);
	// 	}
	// 	if(isset($_GET['teacher_name'])){
	// 		$data['teacher_name']=urldecode($_GET['teacher_name']);
	// 	}
	// 	if(isset($_GET['class_id'])){
	// 		$data['class_id']=urldecode($_GET['class_id']);
	// 	}
	// 	if(isset($_GET['teacher_id'])){
	// 		$data['teacher_id']=urldecode($_GET['teacher_id']);
	// 	}
	// 	$this -> load ->view("home/check_class",$data);
	// }
	// function ClassTeacherList(){
	// 	$connar['items']="db_admin.username teacher_name,db_class.*";
	// 	$connar['join']=array("db_admin","db_admin.id=db_class.teacher_id",'right1');		
	// 	$result=$this -> Class_model ->get_list_full($connar);
	// 	exit(retJson('返回教师列表',true,$result));
	// }
	// function SearchClass(){
	// 	$connar['items']="db_class.*,a.username teacher_name";
	// 	$connar['join']=array('db_admin a','a.id=db_class.teacher_id');
	// 	$connar['where']='1=1 and teacher_id=20080024';
	// 	if(isset($_POST['teacher_id'])){
	// 		$connar['where'].=$this ->sqlEscape(' and db_class.teacher_id=??',array($_POST['teacher_id']));
	// 	}
	// 	if(isset($_POST['date']) &&$_POST['date']>-1){
	// 		$date=strtotime($_POST['date']);
	// 		$connar['where'].=$this ->sqlEscape(' and db_class.created=??',array($date));
	// 	}
	// 	$result=$this -> Class_model ->get_list_full($connar);
	// 	exit(retJson('查询成功',true,$result));
	// }

	// function getClass(){
	// 	// $class=$this->class_service->getClass(time(),true);
	// 	$connar['items']="db_class.*,db_admin.username";
	// 	$connar['join']=array("db_admin","db_admin.id=db_class.teacher_id");
	// 	$class=$this -> Class_model ->get_list_full($connar);
	// 	exit(retJson("课程数据", true,$class));
	// }




	// function searchClassChange($is_default=false){
	// 	if($is_default){
	// 		$connar1['items']="id,username";
	// 		$connar1['where']=$this -> sqlEscape("role_id=?? or find_in_set(??,sub_role_id)",array(WebRole::Teacher[XPHEnum::Value],WebRole::Teacher[XPHEnum::Value]));
	// 		$connar1['orderby']="CONVERT(username USING GBK) ASC ";
	// 		$result['teacher_list']=$this -> Admin_model ->get_list_full($connar1);
	// 	}else{
	// 		$date=date('Y-m-d',time());			
	// 		$month=date('m',time());
	// 		if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
	// 			$year=date('Y',time());
	// 			$year_last=date('Y',time())+1;
	// 			$default_academic_year=strval($year.'-'.$year_last);			
	// 		}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
	// 			$year=date('Y',time())-1;
	// 			$year_last=date('Y',time());
	// 			$default_academic_year=strval($year.'-'.$year_last);	
	// 		}
	// 		$connar['items']="db_class.id,db_class.name,db_class.classroom,db_class.classname,db_class.teacher_id,db_admin.username teacher_name,sksj";
	// 		$connar['where']=$this->sqlLikeEscape('elective_lesson_no like "%??%"',array($default_academic_year));
	// 		$connar['join']=array("db_admin","db_admin.usercode=db_class.teacher_id");
	// 		if(isset($_POST['teacher_name']) && $_POST['teacher_name']!='选择授课教师'){
	// 			$connar['where'].=$this->sqlEscape(" and db_admin.username=?? ",array($_POST['teacher_name']));
	// 		}
	// 		if(isset($_POST['date']) && $_POST['date']!=''){
	// 			$connar['where'].=$this->sqlEscape(" and start_time<=?? and end_time>=??",array($_POST['date'],$_POST['date']));
	// 		}
	// 		if(isset($_POST['weekday']) && $_POST['weekday']!=''){
	// 			$connar['where'].=$this->sqlLikeEscape(" and sksj like '%??%' ",array($_POST['weekday']));
	// 		}
	// 		if(isset($_POST['term']) && $_POST['term']!=''){
	// 			if($_POST['term']=='第一学期'){
	// 				$term='1';
	// 			}else{
	// 				$term='2';
	// 			}
	// 			$connar['where'].=$this->sqlEscape(" and SUBSTRING(elective_lesson_no, 12,1)=?? ",array($term));
	// 		}
	// 		$result['list']= $this -> Class_model ->get_list_full($connar);
	// 		foreach ($result['list'] as $k => $v) {
	// 			$sksj[$k]=explode(';',$v['sksj']);
	// 			$classroom[$k]=explode(';',$v['classroom']);
	// 			foreach ($sksj[$k] as $k1 => $v1) {
	// 					// if($_POST['weekday']!=''){
	// 					// 	if(strpos($v1,$_POST['weekday']) !== false){
	// 				$result['list'][$k]['classroom_d']=$classroom[$k][$k1];	
	// 					// 	}
	// 					// }else{
	// 					// 	$result['list'][$k]['classroom_d']=$classroom[$k][$k1];	
	// 					// }						
	// 			}
	// 		}
	// 	}
	// 	exit(retJson("查询成功",true,$result));
	// }

	// function checkClass(){
	// 	$date=date('Y-m-d',time());			
	// 	$month=date('m',time());
	// 	if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
	// 		$year=date('Y',time());
	// 		$year_last=date('Y',time())+1;
	// 		$default_academic_year=strval($year.'-'.$year_last);			
	// 	}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
	// 		$year=date('Y',time())-1;
	// 		$year_last=date('Y',time());
	// 		$default_academic_year=strval($year.'-'.$year_last);	
	// 	}
	// 	// 获取教师
	// 	$connar['where']=$this->sqlLikeEscape('elective_lesson_no like "%??%" and name="??" and teacher_id=?? and classname="??" ',array($default_academic_year,$_POST['name'],$_POST['teacher_id'],$_POST['classname']));
	// 	$data=$this->Class_model->get_single_full($connar);
	// 	if($data){
	// 		exit(retJson("查询成功",true,$data));
	// 	}else{
	// 		exit(retJson("不存在该课程",false));
	// 	}
	// }
	// function diffBetweenTwoDays ($day1, $day2)
	// {
	// 	$second1 = strtotime($day1);
	// 	$second2 = strtotime($day2);

	// 	if ($second1 < $second2) {
	// 		$tmp = $second2;
	// 		$second2 = $second1;
	// 		$second1 = $tmp;
	// 	}
	// 	return ($second1 - $second2) / 86400;
	// }
	// function checkSubrole(){
	// 	$this -> load ->model("Admin_model");
	// 	$connar['items']='id,usercode,username,role_id,sub_role_id';
	// 	$connar['where']=$this->sqlEscape("id=?? ",array($_SESSION[SESS_USER_HOME]['id']));
	// 	$result=$this-> Admin_model ->get_single_full($connar);
	// 	// var_dump($this->db->last_query());
	// 	// exit();
	// 	if($result)
	// 	{
	// 		$_SESSION[SESS_USER_HOME]=$result;
	// 		if($_SESSION[SESS_USER_HOME]['sub_role_id']!=null ||$_SESSION[SESS_USER_HOME]['sub_role_id']!=''){
	// 			$data['check_role']=1;
	// 			$html="<select id='sub_role' class='input'><option value='0' checked>请选择反馈身份</option>";
	// 			$sub_role=explode(',',$result['sub_role_id']);
	// 			array_push($sub_role,$result['role_id']);
	// 			$count=0;
	// 			foreach ($sub_role as $k=> $v) {
	// 				if($v==WebRole::SchoolSupervisor[XPHEnum::Value] || $v==WebRole::UnitSupervisor[XPHEnum::Value]){
	// 					$count++;
	// 				}

	// 			}
	// 			if($count>1){
	// 				foreach ($sub_role as $k => $v) {
	// 					$role_name=(new WebRole)->show($v);
	// 					if($v==WebRole::SchoolSupervisor[XPHEnum::Value]||$v==WebRole::UnitSupervisor[XPHEnum::Value]){
	// 						$html=$html.'<option value='.$v.'>'.$role_name.'</option>';
	// 					}
	// 				}
	// 				$html=$html."</select>";
	// 				$data['html']=$html;
	// 				exit(retJson("请选择反馈身份！",true,$data));
	// 			}
				
	// 		}
	// 	}
	// }
	function saveAttend($attend_id=0){


        // var_dump($_POST);
		$is_submit=$this -> Attend_model -> get_single(array('id'=>$attend_id));
		if($is_submit['status']==AttendStatus::Submit[XPHEnum::Value]){
			exit(retJson('已提交无法修改',false));
		}
		$formdata=$_POST;
		$data=$this -> Class_model->get_single(array('id'=>$formdata['class_id']));
		$data1=$this -> Admin_model->get_single(array('id'=>$formdata['teacher_id']));
		$data2=$this -> Admin_model->get_single(array('id'=>$_SESSION[SESS_USER_HOME]['id']));
		$class_id=$data['id'];
		
		// if($formdata['sub_role']==0){
		// 	$Error['error']=3;
		// 	exit(retJson('请选择反馈身份！',false,$Error));
		// }else{
		// 	$type=$formdata['sub_role'];
		// }		
		// if($type==WebRole::SchoolSupervisor[XPHEnum::Value]){
		// 	$type=AttendType::School[XPHEnum::Value];
		// }else if($type==WebRole::UnitSupervisor[XPHEnum::Value]){
		// 	$type=AttendType::Unit[XPHEnum::Value];
		// // }
		if($formdata['role']==AttendType::School[XPHEnum::Caption]){
			$type=AttendType::School[XPHEnum::Value];
		}else if($formdata['role']==AttendType::Unit[XPHEnum::Caption]){
			$type=AttendType::Unit[XPHEnum::Value];
		}else if($formdata['role']==AttendType::Outside[XPHEnum::Caption]){
			$type=AttendType::Outside[XPHEnum::Value];
		}
		// $serial_no=$formdata['attend1'];
		$academic_year=$formdata['attend2'];
		$semester=$formdata['attend3'];
		// $class_name=$data['name'];
		// $class_type=$data['type'];
		// $teacher_name=$data1['username'];
		// $title=$data1['title'];
		// $dept_name=$data1['dept_name'];
		// $age_groups=$data1['age_groups'];
		// $teachers_attribute=$data1['teachers_attribute'];
		// $listen_name=$data2['username'];
		// $listen_address=$data['classroom'];
		// $course_units=$data['course_units'];
		$listen_class=$formdata['attend4'];
		$advice=$formdata['attend5'];

		$class_name=$formdata['class_name'];
		$class_type=$formdata['class_type'];
		$teacher_name=$formdata['teacher_name'];
		$teacher_code=$formdata['teacher_code'];
		$title=$formdata['title'];
		$dept_name=$formdata['dept_name'];
		$age_groups=$formdata['age_groups'];
		$teachers_attribute=$formdata['teachers_attribute'];
		$listen_name=$data2['username'];
		$listen_address=$formdata['listen_address'];
		$course_units=$formdata['course_units'];
		$class_id=$formdata['class_id'];
		
		// $age_groups=(new AgeGroups)->show($age_groups);
		// $teachers_attribute=(new TeachersAttribute)->show($teachers_attribute);
		//教学水平
		$teachingLevel=$formdata['teachingLevel'];
		foreach ($teachingLevel as $k => $v) {
			if($v=='请打分' ||$v==''){
				$Error['error']=1;
				exit(retJson("请选择完整的教学水平分数",false,$Error));
			}
		}
		$teachingLevel_rank=array('0','0','0','0','0');
		$teachingLevel_count=0;		
		foreach ($teachingLevel as $k => $v) {
			$teachingLevel_count=$teachingLevel_count+$teachingLevel[$k];			
		}
		if($teachingLevel_count>=90){
			$teachingLevel_rank[0]='1';
		}else if($teachingLevel_count>=80 and $teachingLevel_count<90){
			$teachingLevel_rank[1]='1';
		}else if($teachingLevel_count>=70 and $teachingLevel_count<80){
			$teachingLevel_rank[2]='1';
		}else if($teachingLevel_count>=60 and $teachingLevel_count<70){
			$teachingLevel_rank[3]='1';
		}else if($teachingLevel_count<60){
			$teachingLevel_rank[4]='1';			
		}

		//教学效果
		$teachingEffect=$formdata['teachingEffect'];
		foreach ($teachingEffect as $k => $v) {
			if($v=='0'||$v=='' ){
				$Error['error']=2;
				exit(retJson("请选择完整的教学效果分数",false,$Error));
			}
		}
		$teachingEffect_rank=array('0','0','0','0','0');
		$teachingEffect_count=0;		
		foreach ($teachingEffect as $k => $v) {
			$teachingEffect_count=$teachingEffect_count+$teachingEffect[$k];				
		}
		if($teachingEffect_count>=90){
			$teachingEffect_rank[0]='1';
		}else if($teachingEffect_count>=80 and $teachingEffect_count<90){
			$teachingEffect_rank[1]='1';
		}else if($teachingEffect_count>=70 and $teachingEffect_count<80){
			$teachingEffect_rank[2]='1';
		}else if($teachingEffect_count>=60 and $teachingEffect_count<70){
			$teachingEffect_rank[3]='1';
		}else if($teachingEffect_count<60){
			$teachingEffect_rank[4]='1';			
		}
		if(isset($formdata['teachingReadiness'])){
			$teachingReadiness=$formdata['teachingReadiness'];
		}else{
			$teachingReadiness=array();
		}
		$studentsNorm=$formdata['studentsNorm'];
		$classroom_facilities=$formdata['classroom_facilities'];
		$classroom_environmental=$formdata['classroom_environmental'];
        // foreach ((new ClassroomFacilities)->getAll()as $k => $v) {
        // 	if($classroom_facilities==$v[XPHEnum::Value]){
        // 		$classroom_facilities_item=$v[XPHEnum::Value];
        // 	}
        // }
        // foreach ((new ClassroomEnvironmental)->getAll()as $k => $v) {
        // 	if($classroom_environmental==$v[XPHEnum::Value]){
        // 		$classroom_environmental_item=$v[XPHEnum::Value];
        // 	}
        // }
   //      $attend_record=$formdata['attend_record'];
   //      $recordcount=$formdata['recordcount'];
   //      $attend_record_value=array();
   //      $k =0;
   //      for ($i=1;$i<=$recordcount;$i++){
			// //$item = array('time'=>$attend_record[$i-1].",activity"=>$attend_record[$i].",commentary"=>$attend_record[$i+1].")";			
   //      	$item['time'] =strtotime($attend_record[$k]);
   //      	$item['activity'] =$attend_record[$k+1];
   //      	$item['commentary'] =$attend_record[$k+2];
   //      	array_push($attend_record_value, $item);
   //      	$k+=3;
   //      }

        // if(!isset($this->NOTXSS_POST['attend_img'])){
        // 	exit(retJson("请上传图片",false));
        // }
        // foreach ($this->NOTXSS_POST['attend_img'] as $k => $v) {
        // 	if($v!=null ||$v!=''){
        // 		$attend_img=$this->NOTXSS_POST['attend_img'];
        // 	}else{
        // 		exit(retJson("请上传图片",false));
        // 	}
        // }

		$attend_img_value=array();
		if(isset($this->NOTXSS_POST['attend_img'])){
			$attend_img=$this->NOTXSS_POST['attend_img'];
			for ($i=0;$i<=count($attend_img)-1;$i++){
				$item['img'] = $attend_img[$i];
				if (checkStringIsBase64($item['img'])) {

					$checkRet = uploadImg($item['img'], 'attend_img');
					if (!$checkRet['status']) {
						exit(toRetJson($checkRet));
					}
					$data['img']=$checkRet["v"];

				} else {
					exit(retJson('上传的图片格式不正确！', false));
				}
				array_push($attend_img_value, $data); 
			}
		}
		$is_add=$formdata['is_add'];
		if($formdata['save']==AttendStatus::Submit[XPHEnum::Value]){
			$status=AttendStatus::Submit[XPHEnum::Value];
		}else{
			$status=AttendStatus::Save[XPHEnum::Value];
		}
		// 0415新需求教学准备开展情况可选
		foreach ($formdata['attend_teaching_readiness_content'] as $k => $v) {
			if(!isset($teachingReadiness[$k])){
				$teachingReadiness[$k]='';
			}
		}
		// 0415新需求学生常态可选
		foreach ($formdata['attend_students_norm_item'] as $k => $v) {
			if(!isset($studentsNorm[$k])){
				$studentsNorm[$k]='';
			}
		}
		$param=array(
			'attend'=>array(
				'class_id'=>$class_id,
				'listen_id'=>$_SESSION[SESS_USER_HOME]['id'],
				'type'=>$type,
				// 'serial_no'=>$serial_no,
				'academic_year'=>$academic_year,//学年
				'semester'=>$semester,//学期
				'class_name'=>$class_name,
				'class_type'=>$class_type,
				'teacher_name'=>$teacher_name,
				'teacher_code'=>$teacher_code,
				'title'=>$title,
				'dept_name'=>$dept_name,
				'age_groups'=>$age_groups,
				'teachers_attribute'=>$teachers_attribute,
				'listen_name'=>$listen_name,
				'listen_time'=>$formdata['listen_time'],
				'listen_address'=>$listen_address,
				'course_units'=>$course_units,
				'listen_class'=>$listen_class,
				// 'classroom_facilities'=>$classroom_facilities_item,
				// 'classroom_environmental'=>$classroom_environmental_item,
				'advice'=>$advice,
				'teaching_level_score'=>$teachingLevel_count,
				'teaching_effect_score'=>$teachingEffect_count,
				'status'=>$status,
				// 0415新增总分 教学70%+学生30%
				'score'=>($teachingLevel_count * 0.7+ $teachingEffect_count*0.3)
			),
			'attend_teaching_content'=>array(
				0=>array('key_index'=>$formdata['attend_teaching_index'][0],'list_id'=>$formdata['attend_teaching_list_id'][0],'content'=>$formdata['attend_teaching_content'][0],'score'=>$formdata['attend_teaching_score'][0],'get_score'=>$teachingLevel[0]),
				1=>array('key_index'=>$formdata['attend_teaching_index'][1],'list_id'=>$formdata['attend_teaching_list_id'][1],'content'=>$formdata['attend_teaching_content'][1],'score'=>$formdata['attend_teaching_score'][0],'get_score'=>$teachingLevel[1])
				// ,
				// 2=>array('key_index'=>$formdata['attend_teaching_index'][2],'list_id'=>$formdata['attend_teaching_list_id'][2],'content'=>$formdata['attend_teaching_content'][2],'score'=>$formdata['attend_teaching_score'][0],'get_score'=>$teachingLevel[2])
			),
			'attend_teaching_implementation'=>array(
				0=>array('key_index'=>$formdata['attend_teaching_implementation_index'][0],'list_id'=>$formdata['attend_teaching_implementation_list_id'][0],'content'=>$formdata['attend_teaching_implementation_content'][0],'score'=>$formdata['attend_teaching_implementation_score'][0],'get_score'=>$teachingLevel[2]),
				1=>array('key_index'=>$formdata['attend_teaching_implementation_index'][1],'list_id'=>$formdata['attend_teaching_implementation_list_id'][1],'content'=>$formdata['attend_teaching_implementation_content'][1],'score'=>$formdata['attend_teaching_implementation_score'][1],'get_score'=>$teachingLevel[3])
				// ,
				// 2=>array('key_index'=>$formdata['attend_teaching_implementation_index'][2],'list_id'=>$formdata['attend_teaching_implementation_list_id'][2],'content'=>$formdata['attend_teaching_implementation_content'][2],'score'=>$formdata['attend_teaching_implementation_score'][2],'get_score'=>$teachingLevel[5]),
				// 3=>array('key_index'=>$formdata['attend_teaching_implementation_index'][3],'list_id'=>$formdata['attend_teaching_implementation_list_id'][3],'content'=>$formdata['attend_teaching_implementation_content'][3],'score'=>$formdata['attend_teaching_implementation_score'][3],'get_score'=>$teachingLevel[6]),
				// 4=>array('key_index'=>$formdata['attend_teaching_implementation_index'][4],'list_id'=>$formdata['attend_teaching_implementation_list_id'][4],'content'=>$formdata['attend_teaching_implementation_content'][4],'score'=>$formdata['attend_teaching_implementation_score'][4],'get_score'=>$teachingLevel[7]),
				// 5=>array('key_index'=>$formdata['attend_teaching_implementation_index'][5],'list_id'=>$formdata['attend_teaching_implementation_list_id'][5],'content'=>$formdata['attend_teaching_implementation_content'][5],'score'=>$formdata['attend_teaching_implementation_score'][5],'get_score'=>$teachingLevel[8])	
			),
			'attend_teaching_level'=>array(
				0=>array('item'=>$formdata['attend_teaching_level_item'][0],'remark'=>$formdata['attend_teaching_level_remark'][0],'is_check'=>$teachingLevel_rank[0]),
				1=>array('item'=>$formdata['attend_teaching_level_item'][1],'remark'=>$formdata['attend_teaching_level_remark'][1],'is_check'=>$teachingLevel_rank[1]),
				2=>array('item'=>$formdata['attend_teaching_level_item'][2],'remark'=>$formdata['attend_teaching_level_remark'][2],'is_check'=>$teachingLevel_rank[2]),
				3=>array('item'=>$formdata['attend_teaching_level_item'][3],'remark'=>$formdata['attend_teaching_level_remark'][3],'is_check'=>$teachingLevel_rank[3]),
				4=>array('item'=>$formdata['attend_teaching_level_item'][4],'remark'=>$formdata['attend_teaching_level_remark'][4],'is_check'=>$teachingLevel_rank[4])
			),
			'attend_student_status'=>array(
				0=>array('key_index'=>$formdata['attend_student_status_index'][0],'list_id'=>$formdata['attend_student_status_list_id'][0],'content'=>$formdata['attend_student_status'][0],'score'=>$formdata['attend_student_status_score'][0],'get_score'=>$teachingEffect[0])
				// ,
				// 1=>array('key_index'=>$formdata['attend_student_status_index'][1],'list_id'=>$formdata['attend_student_status_list_id'][1],'content'=>$formdata['attend_student_status'][1],'score'=>$formdata['attend_student_status_score'][1],'get_score'=>$teachingEffect[1]),
				// 2=>array('key_index'=>$formdata['attend_student_status_index'][2],'list_id'=>$formdata['attend_student_status_list_id'][2],'content'=>$formdata['attend_student_status'][2],'score'=>$formdata['attend_student_status_score'][2],'get_score'=>$teachingEffect[2]),
				// 3=>array('key_index'=>$formdata['attend_student_status_index'][3],'list_id'=>$formdata['attend_student_status_list_id'][3],'content'=>$formdata['attend_student_status'][3],'score'=>$formdata['attend_student_status_score'][3],'get_score'=>$teachingEffect[3]),
				// 4=>array('key_index'=>$formdata['attend_student_status_index'][4],'list_id'=>$formdata['attend_student_status_list_id'][4],'content'=>$formdata['attend_student_status'][4],'score'=>$formdata['attend_student_status_score'][4],'get_score'=>$teachingEffect[4]),
				// 5=>array('key_index'=>$formdata['attend_student_status_index'][5],'list_id'=>$formdata['attend_student_status_list_id'][5],'content'=>$formdata['attend_student_status'][5],'score'=>$formdata['attend_student_status_score'][5],'get_score'=>$teachingEffect[5])
			),
			'attend_teaching_effect'=>array(
				0=>array('item'=>$formdata['attend_student_status_item'][0],'remark'=>$formdata['attend_student_status_remark'][0],'is_check'=>$teachingEffect_rank[0])
				// ,
				// 1=>array('item'=>$formdata['attend_student_status_item'][1],'remark'=>$formdata['attend_student_status_remark'][1],'is_check'=>$teachingEffect_rank[1]),
				// 2=>array('item'=>$formdata['attend_student_status_item'][2],'remark'=>$formdata['attend_student_status_remark'][2],'is_check'=>$teachingEffect_rank[2]),
				// 3=>array('item'=>$formdata['attend_student_status_item'][3],'remark'=>$formdata['attend_student_status_remark'][3],'is_check'=>$teachingEffect_rank[3]),
				// 4=>array('item'=>$formdata['attend_student_status_item'][4],'remark'=>$formdata['attend_student_status_remark'][4],'is_check'=>$teachingEffect_rank[4])
			),
			'attend_teaching_readiness'=>array(
				0=>array('item'=>$formdata['attend_teaching_readiness_content'][0],'option'=>$formdata['attend_teaching_readiness_option'][0],'check_value'=>$teachingReadiness[0]),
				1=>array('item'=>$formdata['attend_teaching_readiness_content'][1],'option'=>$formdata['attend_teaching_readiness_option'][1],'check_value'=>$teachingReadiness[1]),
				2=>array('item'=>$formdata['attend_teaching_readiness_content'][2],'option'=>$formdata['attend_teaching_readiness_option'][2],'check_value'=>$teachingReadiness[2]),
				3=>array('item'=>$formdata['attend_teaching_readiness_content'][3],'option'=>$formdata['attend_teaching_readiness_option'][3],'check_value'=>$teachingReadiness[3]),
				4=>array('item'=>$formdata['attend_teaching_readiness_content'][4],'option'=>$formdata['attend_teaching_readiness_option'][4],'check_value'=>$teachingReadiness[4]),
				5=>array('item'=>$formdata['attend_teaching_readiness_content'][5],'option'=>$formdata['attend_teaching_readiness_option'][5],'check_value'=>$teachingReadiness[5]),
				6=>array('item'=>$formdata['attend_teaching_readiness_content'][6],'option'=>$formdata['attend_teaching_readiness_option'][6],'check_value'=>$teachingReadiness[6]),
				7=>array('item'=>$formdata['attend_teaching_readiness_content'][7],'option'=>$formdata['attend_teaching_readiness_option'][7],'check_value'=>$teachingReadiness[7]),
				8=>array('item'=>$formdata['attend_teaching_readiness_content'][8],'option'=>$formdata['attend_teaching_readiness_option'][8],'check_value'=>$teachingReadiness[8]),
				9=>array('item'=>$formdata['attend_teaching_readiness_content'][9],'option'=>$formdata['attend_teaching_readiness_option'][9],'check_value'=>$teachingReadiness[9]),
				10=>array('item'=>$formdata['attend_teaching_readiness_content'][10],'option'=>$formdata['attend_teaching_readiness_option'][10],'check_value'=>$teachingReadiness[10]),
				11=>array('item'=>$formdata['attend_teaching_readiness_content'][11],'option'=>$formdata['attend_teaching_readiness_option'][11],'check_value'=>$teachingReadiness[11])
			),
			'attend_students_norm'=>array(
				0=>array('item'=>$formdata['attend_students_norm_item'][0],'check_value'=>$studentsNorm[0]),
				1=>array('item'=>$formdata['attend_students_norm_item'][1],'check_value'=>$studentsNorm[1]),
				2=>array('item'=>$formdata['attend_students_norm_item'][2],'check_value'=>$studentsNorm[2]),
				3=>array('item'=>$formdata['attend_students_norm_item'][3],'check_value'=>round($studentsNorm[3],2).'%'),
				4=>array('item'=>$formdata['attend_students_norm_item'][4],'check_value'=>$studentsNorm[4]),
				5=>array('item'=>$formdata['attend_students_norm_item'][5],'check_value'=>$studentsNorm[5]),
				6=>array('item'=>$formdata['attend_students_norm_item'][6],'check_value'=>$studentsNorm[6]),
				7=>array('item'=>$formdata['attend_students_norm_item'][7],'check_value'=>$studentsNorm[7]),
				8=>array('item'=>$formdata['attend_students_norm_item'][8],'check_value'=>$studentsNorm[8])				
			),
			'attend_classroom_facilities'=>array('option'=>$formdata['attend_classroom_facilities_option'],'check_value'=>$formdata['classroom_facilities']),
			'attend_classroom_environmental'=>array('option'=>$formdata['classroom_environmental_option'],'check_value'=>$formdata['classroom_environmental']),
			'attend_record'=> array(
				0=>array('time'=>$formdata['attend_record_time'],'activity'=>null,'commentary'=>$formdata['attend_record_content'])
			),
			'attend_img'=>$attend_img_value
		);

// exit;
		if($is_add==0){
			$result=$this->attend_service->saveAttend($param);
		}else{
			$result=$this->attend_service->saveAttend($param,$is_add);//1.id,2,
		}
		unset($_SESSION[SESS_ASSESS]);
		if($formdata['save']==AttendStatus::Submit[XPHEnum::Value]){
			exit(retJson('提交成功！', true,$result));
		}else{
			exit(retJson('保存成功！', true,$result));
		}
		
	}

	function SubmittedAssess(){
		$connar['where']=$this->sqlEscape("listen_id=?? and status<>??",array($_SESSION[SESS_USER_HOME]['id'],AttendStatus::Deleted[XPHEnum::Value]));
		$connar['orderby']='created desc';
		$result['detail'] = $this -> Attend_model ->get_list_full($connar);
		foreach ($result['detail'] as $k => $v) {
			$result['detail'][$k]['type_char']=(new AttendType)->show($v['type']);
		}
		// var_dump($result);
		$this-> load ->view("home/submitted_assess",$result);
	}


	function base64EncodeImage ($image_file) {
		$base64_image = '';
		$image_info = getimagesize($image_file);
		$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
		$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
		return $base64_image;
	}

	// function delAssess(){
	// 	$attend_id=$_POST['attend_id'];
	// 	$data=$this->Attend_model->get_single(array('id'=>$attend_id));
	// 	if($data['status']==AttendStatus::Submit[XPHEnum::Value]){
	// 		exit(retJson('已提交无法删除！',false));
	// 	}
	// 	$this -> Attend_model ->update(array("id"=>$attend_id,"status"=>AttendStatus::Deleted[XPHEnum::Value]));
	// 	exit(retJson("删除成功！",true));
	// }

	// function teachweek ($date){
	// 	$timestamp=strtotime($date);
	// 	if (date('m', $timestamp)>=9){
	// 		return date('W', $timestamp)-35;
	// 	}elseif(date('m', $timestamp)<=2){
	// 		return date('W', $timestamp)+18;
	// 	}else{
	// 		return date('W', $timestamp)-7;
	// 	}
	// }

	// 更新表数据教师工号
    // function  updateTeacherCode(){
    //     $connar['items']='db_attend.id attend_id,db_admin.usercode';
    //     $connar['join']=array('db_admin','db_admin.username=db_attend.teacher_name');
    //     $list=$this->Attend_model->get_list_full($connar);
    //     // var_dump($list);
    //     foreach ($list as $k => $v) {
    //        $update=array('id'=>$v['attend_id'],
    //                     'teacher_code'=>$v['usercode']
    //                 );
    //        $this->Attend_model->update($update);
    //     }
    // }

    // function clearSession(){
    // 	 unset($_SESSION[SESS_ASSESS]);
    // 	 exit(retJson("清除成功",true));
    // }

    // // 保存听课记录session
    // function saveText(){
    // 	$_SESSION[SESS_ASSESS]['record']=$_POST['record'];
    // 	$_SESSION[SESS_ASSESS]['advice']=$_POST['advice'];
    // 	$_SESSION[SESS_ASSESS]['img']=$this->NOTXSS_POST['img'];
    // 	exit(retJson("保存成功",true));
    // }

}